/*
 * Copyright 2012 Netflix, Inc.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.netflix.exhibitor.core.index;

import org.apache.curator.utils.CloseableUtils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.Date;
import java.util.Properties;

public class IndexMetaData
{
    private final Date  from;
    private final Date  to;
    private final int   entryCount;

    private static final String         META_DATA_FILE_EXTENSION = ".properties";

    private static final int        VERSION = 1;
    
    private static final String     PROPERTY_FROM = "from";
    private static final String     PROPERTY_TO = "to";
    private static final String     PROPERTY_COUNT = "count";
    private static final String     PROPERTY_VERSION = "version";

    public static boolean isValid(File indexDirectory)
    {
        return indexDirectory.exists() && indexDirectory.isDirectory();
    }

    public static File getMetaDataFile(File indexDirectory)
    {
        return new File(indexDirectory.getPath() + IndexMetaData.META_DATA_FILE_EXTENSION);
    }

    public static void     write(IndexMetaData meta, File to) throws Exception
    {
        DateFormat      format = DateFormat.getDateTimeInstance();
        
        Properties      properties = new Properties();
        properties.setProperty(PROPERTY_FROM, format.format(meta.from));
        properties.setProperty(PROPERTY_TO, format.format(meta.to));
        properties.setProperty(PROPERTY_VERSION, Integer.toString(VERSION));
        properties.setProperty(PROPERTY_COUNT, Integer.toString(meta.entryCount));
        
        OutputStream    out = new BufferedOutputStream(new FileOutputStream(to));
        try
        {
            properties.store(out, "Auto-generated by Exhibitor");
        }
        finally
        {
            CloseableUtils.closeQuietly(out);
        }
    }

    public static IndexMetaData     read(File from) throws Exception
    {
        DateFormat      format = DateFormat.getDateTimeInstance();

        Properties      properties = new Properties();
        InputStream     in = new BufferedInputStream(new FileInputStream(from));
        try
        {
            properties.load(in);
        }
        finally
        {
            CloseableUtils.closeQuietly(in);
        }

        String version = properties.getProperty(PROPERTY_VERSION, "0");
        if ( !version.equals(Integer.toString(VERSION)) )
        {
            throw new Exception("Unknown version: " + version);
        }

        return new IndexMetaData
        (
            format.parse(properties.getProperty(PROPERTY_FROM)),
            format.parse(properties.getProperty(PROPERTY_TO)),
            Integer.parseInt(properties.getProperty(PROPERTY_COUNT))
        );
    }

    public IndexMetaData(Date from, Date to, int entryCount)
    {
        this.from = from;
        this.to = to;
        this.entryCount = entryCount;
    }

    public Date getFrom()
    {
        return from;
    }

    public Date getTo()
    {
        return to;
    }

    public int getEntryCount()
    {
        return entryCount;
    }
}
